package com.azxcrm.project.system.customer.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.project.system.customer.dao.ICustomerInfoDao;
import com.azxcrm.project.system.customer.dao.ICustomerIntegralRecordDao;
import com.azxcrm.project.system.customer.domain.CustomerIntegralRecord;
import com.azxcrm.project.system.customer.service.ICustomerIntegralRecordService;

/**
 * 积分流水 服务层实现
 * 
 * @author Dustyone
 * @date 2018-11-19
 */
@Service
public class CustomerIntegralRecordServiceImpl implements ICustomerIntegralRecordService 
{
	@Autowired
	private ICustomerIntegralRecordDao customerIntegralRecordDao;
	
	@Autowired
	private ICustomerInfoDao customerInfoDao;
	
	/**
     * 查询积分流水信息
     * 
     * @param id 积分流水ID
     * @return 积分流水信息
     */
	 @Override
	public CustomerIntegralRecord selectCustomerIntegralRecordById(Integer id)
	{
	    return customerIntegralRecordDao.selectCustomerIntegralRecordById(id);
	}
	 
	/**
     * 查询积分流水信息
     * 
     * @param id 积分流水ID
     * @return 积分流水信息
     */
	 @Override
	public CustomerIntegralRecord selectCustomerIntegralRecordByCustomerId(Integer customerId)
	{
	    return customerIntegralRecordDao.selectCustomerIntegralRecordByCustomerId(customerId);
	}
	 
	/**
     * 查询积分流水信息
     * 
     * @param id 积分流水ID
     * @return 积分流水信息
     */
	 @Override
	public CustomerIntegralRecord selectCustomerIntegralRecordByCustomerIdNewRecord(Integer customerId)
	{
	    return customerIntegralRecordDao.selectCustomerIntegralRecordByCustomerIdNewRecord(customerId);
	}
	
	/**
     * 查询积分流水列表
     * 
     * @param customerIntegralRecord 积分流水信息
     * @return 积分流水集合
     */
	 @Override
	public List<CustomerIntegralRecord> selectCustomerIntegralRecordList(CustomerIntegralRecord customerIntegralRecord)
	{
	    return customerIntegralRecordDao.selectCustomerIntegralRecordList(customerIntegralRecord);
	}
	
    /**
     * 新增积分流水
     * 
     * @param customerIntegralRecord 积分流水信息
     * @return 结果
     */
	 @Override
	public int insertCustomerIntegralRecord(CustomerIntegralRecord customerIntegralRecord)
	{
	    return customerIntegralRecordDao.insertCustomerIntegralRecord(customerIntegralRecord);
	}
	
	/**
     * 修改积分流水
     * 
     * @param customerIntegralRecord 积分流水信息
     * @return 结果
     */
	 @Override
	public int updateCustomerIntegralRecord(CustomerIntegralRecord customerIntegralRecord)
	{
	    return customerIntegralRecordDao.updateCustomerIntegralRecord(customerIntegralRecord);
	}
	
	/**
	 * 保存积分流水
	 * 
	 * @param customerIntegralRecord
	 *            积分流水信息
	 * @return 结果
	 */
	@Override
	public int saveCustomerIntegralRecord(CustomerIntegralRecord customerIntegralRecord) {
		Integer id = customerIntegralRecord.getId();
		int rows = 0;

		if (StringUtils.isNotNull(id)) {
			rows = customerIntegralRecordDao.updateCustomerIntegralRecord(customerIntegralRecord);
		} else {
			// 查询最新一条积分流水信息
			CustomerIntegralRecord customerIR = customerIntegralRecordDao
					.selectCustomerIntegralRecordByCustomerIdNewRecord(customerIntegralRecord.getCustomerId());
			customerInfoDao.selectCustomerInfoById(customerIntegralRecord.getCustomerId());
//			 customerIR.getIntegralTotal() == customerInfo.getIntegral()
				// 判断是否为初始录入积分
				if (customerIR == null && customerIntegralRecord.getIntegralType() == 0) {
					customerIntegralRecord.setIntegralTotal(customerIntegralRecord.getIntegralNumber());
				} else if (customerIR != null) {
					int sum = customerIR.getIntegralTotal();
					int number = customerIntegralRecord.getIntegralNumber();
					// 判断是系统充值还是系统扣除(0:充值,1:扣除)
					if (customerIntegralRecord.getIntegralType() == 0) {
						sum += number;
						customerIntegralRecord.setIntegralTotal(sum);
					} else if (customerIntegralRecord.getIntegralType() == 1) {
						sum -= number;
						customerIntegralRecord.setIntegralTotal(sum);
					}
					// 判断是否为初始扣除积分
				} else if (customerIR == null && customerIntegralRecord.getIntegralType() == 1) {
					customerIntegralRecord.setIntegralTotal(0);
				}
				// 积分总数大于0时新增记录
				if (customerIntegralRecord.getIntegralTotal() > 0) {
					rows = customerIntegralRecordDao.insertCustomerIntegralRecord(customerIntegralRecord);
				}
		}
		return rows;
	}
	
	/**
     * 删除积分流水信息
     * 
     * @param id 积分流水ID
     * @return 结果
     */
	 @Override
	public int deleteCustomerIntegralRecordById(Integer id)
	{
	    return customerIntegralRecordDao.deleteCustomerIntegralRecordById(id);
	}
	
	/**
     * 批量删除积分流水对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	 @Override
	public int batchDeleteCustomerIntegralRecord(Integer[] ids)
	{
		return customerIntegralRecordDao.batchDeleteCustomerIntegralRecord(ids);
	}
	
}
